package net.hn.hnms.biz.emgcy.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;
import lombok.EqualsAndHashCode;
import net.hn.hnms.biz.emgcy.domain.RescueTeamPo;
import net.hn.hnms.biz.emgcy.service.RescueTeamService;
import net.hn.hnms.biz.emgcy.template.RescueTeamTemplate;
import net.hn.hnms.sys.system.api.RemoteDictService;
import net.hn.hnms.sys.system.api.domain.SysDictData;


import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @param * @param null
 * @author baijiajun
 * @date 2022/1/20
 * @return
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class RescueTeamListener extends AnalysisEventListener<RescueTeamTemplate> {

    private RescueTeamService rescueTeamService;
    private RemoteDictService remoteDictService;

    private List<RescueTeamTemplate> list = new ArrayList<>();

    public RescueTeamListener(RescueTeamService rescueTeamService, RemoteDictService remoteDictService) {
        this.rescueTeamService = rescueTeamService;
        this.remoteDictService = remoteDictService;
    }

    @Override
    public void invoke(RescueTeamTemplate rescueTeamTemplate, AnalysisContext context) {
        this.list.add(rescueTeamTemplate);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        ArrayList<RescueTeamPo> rescueTeams = new ArrayList<>();
        list.stream().forEach(new Consumer<RescueTeamTemplate>() {
            @Override
            public void accept(RescueTeamTemplate rescueTeamTemplate) {
                try {
                    RescueTeamPo rescueTeam = new RescueTeamPo();
                    rescueTeam.setName(rescueTeamTemplate.getName());
                    SysDictData dict = remoteDictService.queryInfo("team_type", rescueTeamTemplate.getTeamTypeCode());
                    if(null != dict){
                        rescueTeam.setTeamTypeCode(dict.getDictValue());
                    }else{
                        rescueTeam.setTeamTypeCode(null);
                    }
                    SysDictData dictData = remoteDictService.queryInfo("team_attributes_type", rescueTeamTemplate.getQualificationCode());

                    if(null!= dictData){
                        rescueTeam.setQualificationCode(dictData.getDictValue());
                    }else{
                        rescueTeam.setQualificationCode(null);
                    }
                    rescueTeam.setContact(rescueTeamTemplate.getContact());
                    rescueTeam.setContactTel(rescueTeamTemplate.getContactTel());
                    //rescueTeam.setAddress(rescueTeamTemplate.getAddress());
                    rescueTeam.setTotalPerNum(rescueTeamTemplate.getTotalPerNum());
                   /* rescueTeam.setFoundedDate(rescueTeamTemplate.getFoundedDate());
                    rescueTeam.setEquipDesc(rescueTeamTemplate.getEquipDesc());
                    rescueTeam.setSpecialityDesc(rescueTeamTemplate.getSpecialityDesc());
                    rescueTeam.setEmgcyResume(rescueTeamTemplate.getEmgcyResume());*/
                    rescueTeam.setResponsibleUnit(rescueTeamTemplate.getResponsibleUnit());
                    rescueTeam.setDuty(rescueTeamTemplate.getDuty());
                    rescueTeams.add(rescueTeam);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        rescueTeamService.saveBatch(rescueTeams);
        this.list.clear();

    }
}
